package com.nearby.demand.utils;

import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Administrator on 2015/12/23.
 */
public class ViewHolder2 {
    private SparseArray<View> viewHolders;
    private View view;

    /**
     * 静态获取ViewHolder
     * @param view
     * @return
     */
    public static ViewHolder2 getViewHolder(View view) {
        ViewHolder2 viewHolder2 = (ViewHolder2) view.getTag();
        if (viewHolder2 == null) {
            viewHolder2 = new ViewHolder2(view);
            view.setTag(viewHolder2);
        }
        return viewHolder2;
    }

    /**
     * 构建初始化
     * @param view
     */
    private ViewHolder2(View view) {
        this.view = view;
        viewHolders = new SparseArray<View>();
        view.setTag(viewHolders);
    }

    /**
     * 获取控件
     * @param id
     * @param <T>
     * @return
     */
    public <T extends View> T get(int id) {
        View childView = viewHolders.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolders.put(id, childView);
        }
        return (T) childView;
    }
    public View getConvertView() {
        return view;
    }

    public TextView getTextView(int id) {

        return get(id);
    }
    public Button getButton(int id) {

        return get(id);
    }

    public ImageView getImageView(int id) {
        return get(id);
    }

    public void setTextView(int  id,CharSequence charSequence){
        getTextView(id).setText(charSequence);
    }
}
